package org.dllwh.milvus.v1;

import cn.hutool.core.collection.CollectionUtil;
import io.milvus.client.MilvusServiceClient;
import io.milvus.grpc.DescribeDatabaseResponse;
import io.milvus.grpc.ListDatabasesResponse;
import io.milvus.param.R;
import io.milvus.param.RpcStatus;
import io.milvus.param.collection.CreateDatabaseParam;
import io.milvus.param.collection.DescribeDatabaseParam;
import io.milvus.param.collection.DropDatabaseParam;
import lombok.extern.slf4j.Slf4j;

import java.util.Map;

/**
 * 把今天最好的表现当作明天最新的起点．．～
 * <p>
 * Today the best performance as tomorrow the newest starter!
 *
 * @ClassName MilvusDatabaseHelper
 * @Description Milvus 数据库操作
 * @Author: <a href="mailto:duleilewuhen@sina.com">独泪了无痕</a>
 * @Date 2025-05-24 14:34
 * @版本 V 1.0.1
 * @since JDK 17
 */
@Slf4j
public class MilvusDatabaseHelper {
    /**
     * 创建新数据库
     *
     * @param databaseName     指定数据库名称
     * @param databaseProperty 数据库属性
     */
    public static boolean createDatabase(String databaseName, Map<String, String> databaseProperty) {
        MilvusServiceClient milvusClient = MilvusConnectHelper.getMilvusServiceClient();
        CreateDatabaseParam.Builder createDatabaseParam = CreateDatabaseParam.newBuilder().withDatabaseName(databaseName);
        if (CollectionUtil.isNotEmpty(databaseProperty)) {
            databaseProperty.forEach(createDatabaseParam::withProperty);
        }
        R<RpcStatus> response = milvusClient.createDatabase(createDatabaseParam.build());
        if (R.Status.Success.getCode() != response.getStatus()) {
            log.error("创建数据库失败，失败原因是：{}", response.getMessage());
            return false;
        }
        Exception exception = response.getException();
        if (exception != null) {
            log.error("创建数据库失败，失败原因是：{}", exception.getMessage());
            return false;
        }
        return true;
    }

    /**
     * 获取 Milvus 中所有现有数据库
     */
    public static void listDatabases() {
        MilvusServiceClient milvusClient = MilvusConnectHelper.getMilvusServiceClient();
        R<ListDatabasesResponse> response = milvusClient.listDatabases();
        if (R.Status.Success.getCode() != response.getStatus()) {
            log.error("构建查询请求失败，失败原因是：{}", response.getMessage());
        } else {
            Exception exception = response.getException();
            if (exception != null) {
                log.error("获取 Milvus 所有数据库，失败原因是：{}", exception.getMessage());
            } else {
                System.out.println(response.getData());
            }
        }
    }

    /**
     * 查看数据库详细信息
     *
     * @param databaseName 指定数据库名称
     */
    public static void describeCollection(String databaseName) {
        MilvusServiceClient milvusClient = MilvusConnectHelper.getMilvusServiceClient();
        DescribeDatabaseParam.Builder describeDatabaseParam = DescribeDatabaseParam.newBuilder().withDatabaseName(databaseName);
        R<DescribeDatabaseResponse> response = milvusClient.describeDatabase(describeDatabaseParam.build());
        if (R.Status.Success.getCode() != response.getStatus()) {
            log.error("查看数据库【{}】详细信息，失败原因是：{}", databaseName, response.getMessage());
        } else {
            System.out.println(response.getData());
        }
    }

    /**
     * 要丢弃一个数据库，必须先丢弃它的所有集合。否则，删除将失败。
     *
     * @param databaseName 指定数据库名称
     */
    public static void dropDatabase(String databaseName) {
        MilvusServiceClient milvusClient = MilvusConnectHelper.getMilvusServiceClient();
        DropDatabaseParam.Builder dropDatabaseParam = DropDatabaseParam.newBuilder().withDatabaseName(databaseName);
        R<RpcStatus> response = milvusClient.dropDatabase(dropDatabaseParam.build());
        if (R.Status.Success.getCode() != response.getStatus()) {
            log.error("删除数据库【{}】详细信息，失败原因是：{}", databaseName, response.getMessage());
        } else {
            System.out.println(response.getData());
        }
    }
}
